package gaobo.singlelist;

import java.util.Stack;

public class MySingleList {

    //(节点)内部类
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//不初始化，默认值就是null

    public void createList() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;

        this.head = listNode1;
    }

    //打印链表
    //默认从head打印
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //默认从指定位置打印
    public void display(ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //递归打印链表
    public void display2(ListNode head) {
        if (head == null) {
            return;
        }
        if (head.next == null) {
            System.out.print(head.val + " ");
            return;
        }
        display2(head.next);
        System.out.print(head.val + " ");
    }

    //递归转化为循环(非递归)——>使用栈
    //1.递归的结束条件就是【起始条件】
    //2.递推公式
    public void display3(ListNode head) {
        if (head == null) {
            return;
        }
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (!stack.empty()) {
            ListNode ret = stack.pop();
            System.out.print(ret.val + " ");
        }
    }

    //得到单链表长度
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找单链表中是否包含关键字key
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if (cur == null) {
            this.head = node;
        } else {
            while (cur.next != null) {
                cur = cur.next;
            }
            //cur已经是尾节点
            cur.next = node;
        }
    }

    //任意位置插入
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("index位置不合法");
            throw new IndexWrongFulException("index位置不合法");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        //1.先走index-1步，找到cur
        ListNode cur = findIndexSubOne(index);
        ListNode node = new ListNode(data);
        //2.修改指向
        node.next = cur.next;
        cur.next = node;

    }

    private ListNode findIndexSubOne(int index) {
        ListNode cur = this.head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode cur = findPrevOfKey(key);
        if (cur == null) {
            System.out.println("没有你要删除的数字");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;

    }

    private ListNode findPrevOfKey(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除所有关键字key的节点
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key) {
            this.head = this.head.next;
        }
    }

    //清空单链表
    public void clear() {
        this.head = null;
    }

    //反转链表(每年面试必考)
    public ListNode reverseList() {
        if (this.head == null) {
            return null;
        }
        if (head.next == null) {
            return null;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = this.head;
            this.head = cur;
            cur = curNext;
        }
        return this.head;
    }

    //遍历找到链表中间位置(快慢指针,优点只遍历一次)
    public ListNode middleNode() {
        ListNode fast = this.head;
        ListNode slow = this.head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //求链表中倒数第k个节点(只遍历一次)
    public ListNode FindKthToTail(int k) {
        if (k <= 0 || head == null) {
            return null;
        }
        ListNode fast = this.head;
        ListNode slow = this.head;
        while (k - 1 != 0) {
            fast = fast.next;
            if (fast == null) {
                return null;
            }
            k--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    //合并有序列表
    //P.Test

    //链表分割
    //给定一个值x,编写一段代码将所有小于x的结点排在其余结点之前，切不能改变原来的数据顺序
    //返回重新排列后的链表的头指针
    public ListNode partition(ListNode pHead, int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;
        //遍历链表,当cur为null的时候，链表遍历完成
        while (cur != null) {
            if (cur.val < x) {
                //第一次进行插入节点
                if (bs == null) {
                    bs = cur;
                    be = cur;
                } else {
                    //不是第一次进行插入节点
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = cur;
                    ae = cur;

                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        //第一个段没有数据
        if (bs == null) {
            return as;
        }
        be.next = as;
        //手动置空一次
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

    //链表的回文结构
    //判断是否是回文结构
    //1.找到链表的中间节点
    //2.翻转中间节点后面的链表
    //3.一个从头开始，一个从尾巴开始遍历
    public boolean chPalindrome(ListNode head) {
        if (head == null) {
            return false;
        }
        //1.考虑只有一个节点的情况，他一定是回文的结构
        if (head.next == null) {
            return true;
        }
        //2.找到中间节点
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //3.开始进行翻转链表
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //4.开始判断回文
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            //处理偶数情况下
            if (head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    /*非常经典的题*/
    //判断链表中是否有环(追击问题)
    public boolean hasCycle() {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    public void createLoop() {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = head.next.next;
    }

    //如何找到环的入口点？
    public ListNode detectCycle() {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        //两种情况
        //1.循环条件不满足
        //2.遇到break
        if (fast == null || fast.next == null) {
            return null;//代表这个地方没有环
        }
        slow = head;
        while (slow != fast) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

    //单链表的基本操作
    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        mySingleList.addLast(20);
        mySingleList.addLast(30);
        mySingleList.addLast(6);
        mySingleList.addLast(30);
        mySingleList.addLast(20);
        boolean ret = mySingleList.chPalindrome(mySingleList.head);
        System.out.println(ret);
    }
}
        //mySingleList.createList();
//        mySingleList.display();
//        mySingleList.display2(mySingleList.head);
//        mySingleList.display3(mySingleList.head);
        //mySingleList.addIndex(0,9999);
        //mySingleList.remove(56);
        //mySingleList.removeAllKey(12);
        //mySingleList.display();
        //System.out.println("链表长度:" + mySingleList.size());
        //System.out.println(mySingleList.contains(34));
        //mySingleList.clear();
        //MySingleList.ListNode ret = mySingleList.reverseList();
        //mySingleList.display(ret);
        //mySingleList.middleNode();
        //MySingleList.ListNode ret2 = mySingleList.middleNode();
        //MySingleList.ListNode ret = mySingleList.FindKthToTail(4);
        //System.out.println(ret.val);
        //System.out.println(ret2.val);
        //MySingleList.ListNode ret = mySingleList.partition(mySingleList.head, 22);
        //mySingleList.display(ret);
        //boolean ret = mySingleList.chPalindrome(mySingleList.head);
        //System.out.println(ret);

        //mySingleList.createLoop();
//        System.out.println(mySingleList.hasCycle());
//        MySingleList.ListNode ret = mySingleList.detectCycle();
//        System.out.println(ret.val);





